/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerCadastraCliente;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import model.Cliente;
import org.w3c.tidy.ParserImpl;

/**
 *
 * @author Dick developer
 */
public class JCadastraCliente extends javax.swing.JFrame {

    private ControllerCadastraCliente controllercadastracliente;
    private JPrincipal jf;
    private String cpf;
    private Cliente cliente;
    //private Calendar datanasc;
    private Date date;
    private int tamanho;
    private int cont = 1;
    private boolean alterar;
    private int controle;
    private JRecepcao jr;
    
    public JCadastraCliente(ControllerCadastraCliente CCC,JPrincipal jf, int controle) {
        initComponents();
        this.controllercadastracliente = CCC;
        this.jf = jf;
        jComboBox1.setEnabled(false);
        bloqueiaCampo();
        blokeia_editar();
        this.controle = controle;

    }

    public JCadastraCliente(ControllerCadastraCliente CCC,JRecepcao jr,int controle) {
        initComponents();
        this.controllercadastracliente = CCC;
        jComboBox1.setEnabled(false);
        bloqueiaCampo();
        blokeia_editar();
        this.jr = jr;
        this.controle = controle;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jFormattedTextField11 = new javax.swing.JFormattedTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jFormattedTextField7 = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jFormattedTextField13 = new javax.swing.JFormattedTextField();
        jLabel13 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        button1 = new javax.swing.JButton();
        data_nasc = new com.toedter.calendar.JDateChooser();
        jLabel8 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton2MouseExited(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 340, 60, 60));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/editar1.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton1MouseExited(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 130, 30, 30));

        jLabel4.setText("Nome:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 140, -1, -1));
        getContentPane().add(jFormattedTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 140, 110, -1));

        jFormattedTextField11.setText("");
        jFormattedTextField11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextField11ActionPerformed(evt);
            }
        });
        getContentPane().add(jFormattedTextField11, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 180, 110, -1));

        jLabel7.setText("Telefone:");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 180, -1, -1));

        jLabel6.setText("Data de Nasciemento:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 220, -1, -1));

        jLabel5.setText("Sexo:");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 260, -1, -1));

        jLabel3.setText("Documento:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 100, -1, -1));

        jFormattedTextField7.setColumns(14);
        try {
            jFormattedTextField7.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextField7ActionPerformed(evt);
            }
        });
        jFormattedTextField7.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jFormattedTextField7KeyReleased(evt);
            }
        });
        getContentPane().add(jFormattedTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 100, 110, -1));

        jLabel9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
        jLabel9.setText("Cadastro/Alteração Cliente");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 30, -1, -1));

        jLabel2.setText("Foto do Cliente");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 90, -1, -1));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, 210, 10));

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator4, new org.netbeans.lib.awtextra.AbsoluteConstraints(650, 120, 30, 180));

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 120, -1, 180));
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 120, 190, 180));

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/facebook.png"))); // NOI18N
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 300, 30, 30));

        jLabel12.setText("E-mail/Facebook:");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 300, -1, -1));

        jFormattedTextField13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextField13ActionPerformed(evt);
            }
        });
        getContentPane().add(jFormattedTextField13, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 300, 110, -1));

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cpf.jpg"))); // NOI18N
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 100, 40, 30));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 260, 110, -1));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fechar.png"))); // NOI18N
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButton3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jButton3MouseExited(evt);
            }
        });
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 340, 60, 60));

        button1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/xpequeno.png"))); // NOI18N
        button1.setBorderPainted(false);
        button1.setContentAreaFilled(false);
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button1MouseExited(evt);
            }
        });
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });
        getContentPane().add(button1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 90, 30, 30));
        getContentPane().add(data_nasc, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 220, 110, -1));

        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fundotelasinvertido.png"))); // NOI18N
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 700, 440));

        jMenuBar1.setBackground(java.awt.Color.white);

        jMenu1.setText("Ajuda");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Regras da casa");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Sair");

        jMenuItem1.setText("Sair");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseEntered

        jButton2.setSize(70,70);

    }//GEN-LAST:event_jButton2MouseEntered

    private void jButton2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseExited

        jButton2.setSize(60,60);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2MouseExited

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
//        jFormattedTextField3.setEnabled(true);
//        jFormattedTextField11.setEnabled(true);
//        jFormattedTextField12.setEnabled(true);
//        jComboBox1.setEnabled(true);
//        jFormattedTextField13.setEnabled(true);  
        
//        cliente.setDocumento("222.222.222-22");
//        cliente.setNome("Angelo");
//        cliente.setNascimento(new Date());
//        cliente.setSexo("M");
//        cliente.setEmail("dick@dick");
        
        
        if(alterar == false){

            cliente = new Cliente();
            cliente.setDocumento(jFormattedTextField7.getText().toString());
            cliente.setNome(jFormattedTextField3.getText().toString());
            //= datanasc.getDate(jFormattedTextField12.toString());
            cliente.setTelefone(jFormattedTextField11.getText().toString());
            cliente.setNascimento(data_nasc.getDate());
            cliente.setSexo(jComboBox1.getSelectedItem().toString());
            cliente.setEmail(jFormattedTextField13.getText().toString());

                if(controllercadastracliente.cadastra_cliete(cliente)){

                    JOptionPane.showMessageDialog(null, "Novo cliente cadstrado com sucesso! ");
                    zera_tela();

                }

        
        } else if (alterar == true){
            
            //cliente = new Cliente();
            cliente.setDocumento(jFormattedTextField7.getText().toString());
            cliente.setNome(jFormattedTextField3.getText().toString());
            //= datanasc.getDate(jFormattedTextField12.toString());
            cliente.setTelefone(jFormattedTextField11.getText().toString());
            
            cliente.setNascimento(data_nasc.getDate());
            cliente.setSexo(jComboBox1.getSelectedItem().toString());
            cliente.setEmail(jFormattedTextField13.getText().toString());
            
             if(controllercadastracliente.altera_cliente(cliente)){

                    JOptionPane.showMessageDialog(null, "Cliente alterado com sucesso ! ");
                    zera_tela();

                }

        }
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseEntered

        jButton1.setSize(40,40);
        
    }//GEN-LAST:event_jButton1MouseEntered

    private void jButton1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseExited

        jButton1.setSize(30,30);
        
    }//GEN-LAST:event_jButton1MouseExited

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed


        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jFormattedTextField11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextField11ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField11ActionPerformed

    private void jFormattedTextField7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextField7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField7ActionPerformed

    private void jFormattedTextField13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextField13ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField13ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        jf.restaurar(jf);
        this.hide();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jFormattedTextField7KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jFormattedTextField7KeyReleased

        if(jFormattedTextField7.getText().length() == 4){
            cont =1;
        }
        
        while(cont ==1){
            tamanho = jFormattedTextField7.getText().length();
            tamanho = (tamanho-11);
            cont = 2;
        }
        tamanho = (tamanho +1);
        
        //JOptionPane.showMessageDialog(null, tamanho);
        
        if(tamanho >= 14){

            cpf = jFormattedTextField7.getText();
            
            if(controllercadastracliente.verifica_se_usuario_tem_cadastro(cpf)){
                
                // usuario ja cadastrado 
                JOptionPane.showMessageDialog(null, "Este usuario já esta cadastrado no sistema");
                
                String data;
                
                cliente = new Cliente();
                cliente = controllercadastracliente.retorna_dados_do_cliente(cpf);
                jComboBox1.setSelectedItem(cliente.getSexo());
                jFormattedTextField11.setText(cliente.getTelefone());
//                date = new Date();
//                date = cliente.getNascimento();
//                data = date.getDay()+"/"+date.getMonth()+"/"+date.getYear();
                data_nasc.setDate(cliente.getNascimento());
                jFormattedTextField13.setText(cliente.getEmail());
                jFormattedTextField3.setText(cliente.getNome());
                bloqueiaCampo();
                libera_editar();
                
            }else{
                
                jFormattedTextField7.setEnabled(false);
                liberaCampo();
                //JOptionPane.showMessageDialog(null, "Este usuario não tem cadastro ativo no sistema");
                
            }
        
        }
        
        
        
        
    }//GEN-LAST:event_jFormattedTextField7KeyReleased

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        
        
        
        
        
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton3MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseEntered
        jButton3.setSize(70,70);
    }//GEN-LAST:event_jButton3MouseEntered

    private void jButton3MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseExited
       jButton3.setSize(60,60);
    }//GEN-LAST:event_jButton3MouseExited

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
        if(controle == 1){
            jf.restaurar(jf);
            this.hide();    
        }else if(controle == 2){
            jr.restaurar(jr);
            this.hide(); 
        }
        
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        
        
        liberaCampo();

    }//GEN-LAST:event_jButton1MouseClicked

    private void button1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_button1MouseClicked

    private void button1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseEntered
         button1.setSize(40,40);
    }//GEN-LAST:event_button1MouseEntered

    private void button1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_button1MouseExited
       button1.setSize(30,30);
    }//GEN-LAST:event_button1MouseExited

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
       zera_tela();
    }//GEN-LAST:event_button1ActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button1;
    private com.toedter.calendar.JDateChooser data_nasc;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFormattedTextField jFormattedTextField11;
    private javax.swing.JFormattedTextField jFormattedTextField13;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JFormattedTextField jFormattedTextField7;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    // End of variables declaration//GEN-END:variables


public void exibe() {
    setVisible(true);     
}

    private void bloqueiaCampo() {
        jComboBox1.setEnabled(false);
        jFormattedTextField11.setEnabled(false);
        data_nasc.setEnabled(false);
        jFormattedTextField13.setEnabled(false);
        jFormattedTextField3.setEnabled(false);
    }

    private void liberaCampo() {
        jComboBox1.setEnabled(true);
        jFormattedTextField11.setEnabled(true);
        data_nasc.setEnabled(true);
        jFormattedTextField13.setEnabled(true);
        jFormattedTextField3.setEnabled(true);
    }

    private void libera_editar(){
        
        jButton1.setVisible(true);
        alterar = true;
        jFormattedTextField7.setEnabled(false);
        
    }
    private void blokeia_editar(){
        
        jButton1.setVisible(false);
        alterar = false;
        
    }

    private void zera_tela(){
    
        bloqueiaCampo();
        blokeia_editar();
        cont =1;
        jFormattedTextField7.setText("");
        jComboBox1.setSelectedItem("M");
        jFormattedTextField11.setText("");
        data_nasc.setDate(date);
        jFormattedTextField13.setText("");
        jFormattedTextField3.setText("");
        jFormattedTextField7.setEnabled(true);
        
    }


}
